import { reqGoodInfo,reqAddOrUpdateShopCart } from "@/api"
import {getUUID} from '@/pages/utils/uuid_token'
const state={
    getGoodInfo:{},
    //游客临时身份
    uuid_token:getUUID()
}
const mutations={
    GETGOODINFO(state,getGoodInfo){
        state.getGoodInfo=getGoodInfo
    }
}
const actions={
    async getGoodInfo({commit},skuId){
        let result = await reqGoodInfo(skuId)
        if(result.code==200){
            commit('GETGOODINFO',result.data)
        }
    },
    //购物车存数据  不返回数据 下面的{skuId,skuNum}属于解构
    async addOrUpdateShopCart({commit},{skuId,skuNum}){
        let result = await reqAddOrUpdateShopCart(skuId,skuNum)
        //async标记的函数的返回值都是Promise---返回成功或者失败
        if(result.code==200){
            return 'ok'
        }else{
            return Promise.reject(new Error("faile"))
        }
    }

}
const getters={
    //本地小仓库不需要写成state.detail.getGoodInfo.categoryView
    //简化路径导航的数据
    categoryView(state){
        return state.getGoodInfo.categoryView || {}
    },
    //简化产品信息的数据
    skuInfo(state){
        return state.getGoodInfo.skuInfo || {}
    },//简化产品售卖属性的数据
    spuSaleAttrList(state){
        return state.getGoodInfo.spuSaleAttrList||[]
    }
}
export default({
    state,
    mutations,
    actions,
    getters
})
